package cn.tedu.simpleFactory;

public class FruitFactory {
    /*
     *  实例工厂, 创建 水果实例
     */
    /*public static Fruit getApple(){
        return new Apple();
    }
    public static Fruit getOrange(){
        return new Orange();
    }*/

    /**
     *   优化 : 用一个方法封装
     */
    /*public static Fruit get(String type) throws IllegalAccessException, InstantiationException {
        if (type.equalsIgnoreCase("apple")){
            return Apple.class.newInstance();
        }else if (type.equalsIgnoreCase("orange")){
            return Orange.class.newInstance();
        }else {
            System.out.println("找不到类");
            return null;
        }
    }*/
    /**
     *   进一步优化 :
     */
    public static Fruit get(String type) throws ClassNotFoundException, IllegalAccessException, InstantiationException {

        type = type.toLowerCase();
        type = type.substring(0,1).toUpperCase() + type.substring(1);
        System.out.println(type);
        Class<?> fruit = Class.forName("cn.tedu.factory." + type);
        return (Fruit) fruit.newInstance();

    }

}
